package com.zat.mes.andrmes.utils;

import android.app.Activity;

import java.util.ArrayList;

/**
 * Activity管理工具类
 */
public class ActivityUtils {
    /**
     * Activity栈
     */
    public static ArrayList<Activity> activitys = new ArrayList<Activity>();
    /**
     * 是否在清除栈
     */
    public static boolean clearStack;

    /**
     * 存入一个activity
     */
    public static void put(Activity activity) {
        activitys.add(activity);
    }

    /**
     * 根据下标移除activity
     *
     * @param index 下标
     */
    public static void remove(int index) {
        activitys.remove(index);
    }

    /**
     * 根据下标关闭一个activity
     *
     * @param index 下标
     */
    public static void finish(int index) {
        if (index >= activitys.size()) {
        } else {
            Activity activity = activitys.get(index);
            finish(activity);
        }
    }

    /**
     * 关闭Activity
     */
    private static void finish(Activity activity) {
        if (activity != null) {
            activity.finish();
        }
    }

    /**
     * 抛出栈顶Activity，回到主界面
     */
    public static synchronized void finishAllWithoutMain() {
        finishAllSaveIndex(1);
    }

    /**
     * 抛出全部activity
     */
    public static synchronized void finishAll() {
        finishAllSaveIndex(0);
        activitys = null;
    }

    /**
     * 清除除了top的所有activity(登录用)
     */
    public static synchronized void finishWithoutTop() {
        clearStack = true;
        for (int i = 0; i < activitys.size() - 1; i++) {
            Activity activity = activitys.get(i);
            finish(activity);
        }
        removeAllSaveTop(activitys.get(activitys.size() - 1));
    }

    /**
     * 清除栈，保留栈顶
     */
    private static synchronized void removeAllSaveTop(Activity activity) {
        activitys.clear();
        activitys.add(activity);
        clearStack = false;
    }

    /**
     * 抛出栈顶部分Activity，回到指定下标页面
     *
     * @param index 下标位置。例如：保留MainActivity，则应传入index=1;
     */
    public static synchronized void finishAllSaveIndex(int index) {
        clearStack = true;
        for (int i = index; i < activitys.size(); i++) {
            Activity activity = activitys.get(i);
            finish(activity);
        }
        removeAllSaveIndex(index);
    }

    /**
     * 清除栈，保留Main
     */
    private static synchronized void removeAllSaveIndex(int index) {
        ArrayList<Activity> subList = new ArrayList<Activity>();
        for (int i = 0; i < index; i++) {
            subList.add(activitys.get(i));
        }
        activitys.clear();
        activitys.addAll(subList);
        clearStack = false;
    }


    /**
     * 根据类名返回栈中的Activity
     *
     * @param className 类名
     * @return 存在则返回栈中的类实例，反之，返回一个新的Activity实例
     */
    public static Activity getActivityByClsName(String className) {
        for (int i = 0; i < activitys.size(); i++) {
            Activity activity = activitys.get(i);
            String clsName = activity.getClass().getName();
            if (clsName.equals(className)) {
                return activity;
            }
        }
        return new Activity();
    }

    /**
     * 根据类名关闭一个activity
     *
     * @param className 类名
     */
    public static void finish(String className) {
        for (int i = 0; i < activitys.size(); i++) {
            Activity activity = activitys.get(i);// 1,2,3,4,
            String clsName = activity.getClass().getName();
            if (clsName.equals(className)) {
                finish(activity);
                return;
            }
        }
    }

    /**
     * 根据类名关闭一个activity
     *
     * @param className 类名
     */
    public static void finish(String... className) {
        for (String curCls : className) {
            finish(curCls);
        }
    }

    /**
     * 抛出栈顶部分Activity，使应用停留在指定页面
     *
     * @param toClsName 要保留的栈顶类名
     */
    public static void finishTo(String toClsName) {
        for (int i = 0; i < activitys.size(); i++) {
            Activity activity = activitys.get(i);
            if (activity.getClass().getName().equals(toClsName)) {
                finishAllSaveIndex(i + 1);
                return;
            }
        }
    }

    /**
     * 栈中有该Activity
     *
     * @param clsName
     * @return
     */
    public static boolean activityInStack(String clsName) {
        for (Activity activity : activitys) {
            if (clsName.equals(activity.getClass().getName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据类名从栈中移除activity
     *
     * @param className 获取方法：getClass().getName();
     */
    public static void remove(String className) {
        if (!clearStack) {
            for (int i = 0; i < activitys.size(); i++) {// 循环列表
                Activity activity = activitys.get(i);
                String clsName = activity.getClass().getName();
                if (clsName.equals(className)) {
                    remove(i);// 移除
                    break;
                }
            }
        }
    }

    public static boolean has(String className) {
        for (Activity activity : activitys) {
            if (activity.getClass().getName().equals(className)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 结束多个activity
     *
     * @param index 下标数组，可以是多个
     */
    public static void finish(int... index) {
        for (int i = 0; i < index.length; i++) {
            finish(i);
        }
    }

}
